package Demarrage;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Outils {

    public static void creerVignette(int tailleX, int tailleY, File fichier,String dest) {

        File apercuPhoto = new File(dest + fichier.hashCode());

        if (apercuPhoto.exists()) return;

        
        BufferedImage vignetteTemp;
        BufferedImage image = null;
        try {
            image = ImageIO.read(fichier);
        } catch (IOException ex) {}


        // On retaille en gardant les proportions
        
        int longueurImage = image.getWidth();
        int largeurImage = image.getHeight();
        
        if (longueurImage >= largeurImage) vignetteTemp = retaillerImage(image, tailleX, largeurImage * tailleX / longueurImage);
        else vignetteTemp = retaillerImage(image, longueurImage * tailleY / largeurImage, tailleY);
       
        File destination = new File(dest + fichier.hashCode());
        
        try {
            ImageIO.write(vignetteTemp, "JPEG", destination);
        } catch (IOException ex) {}
        Image vignette = vignetteTemp;


    }

    private static BufferedImage retaillerImage(BufferedImage srcImg, int w, int h) {
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();

        return resizedImg;
    }
}
